package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.entity.EmployeeDTO;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody EmployeeDTO employee, HttpServletRequest request) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee em = employeeService.getOne(queryWrapper);
        if (em == null) {
           return R.error("登录失败");
        }
        if (!em.getPassword().equals(password)) {
            return R.error("登录失败");
        }
        if (em.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        HttpSession session = request.getSession();
        session.setAttribute("employee", em.getId());
        return R.success(em);
    }
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpServletRequest request) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.save(employee);
        return R.success("保存成功");
    }

    @GetMapping("/page")
    public R<IPage<Employee>> page(String name, int page, int pageSize) {
        IPage<Employee> employeeIPage = employeeService.selectPage(name, page, pageSize);
        return R.success(employeeIPage);
    }

    @PutMapping
    public R<String> updateEmploy(@RequestBody Employee employee, HttpServletRequest request) {
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
//        employeeService.updateEmploy(employee);
        return R.success("更新成功");
    };

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.selectById(id);
        return R.success(employee);
    };
}
